/**
 * 
 */
package com.alkandros.myloke.activities;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.Spinner;

import com.alkandros.myloke.R;
import com.alkandros.myloke.managers.AppPreferenceManager;
import com.alkandros.myloke.managers.HeaderManager;
import com.alkandros.myloke.models.FaithModel;

/**
 * @author Jiju Induchoodan
 * 
 */
public class SettingsActivity extends Activity implements OnClickListener,OnCheckedChangeListener
{

	private RelativeLayout relativeLayoutHeader;
	private HeaderManager headerManager;
	/*private RadioButton radioChrisitian;
	private RadioButton radioJudasim;
	private RadioButton radioBuddhism;
	private RadioButton radioSikhism;
	private RadioButton radioHinduism;
	private RadioButton radioIslam;*/
	
	private LinearLayout tableLayout;
	private Spinner spinnerMiles;

//	private RadioGroup radioGroupLeft;
//	private RadioGroup radioGroupRight;
	 RadioGroup rg1 ; 
	 RadioGroup rg2 ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);

		relativeLayoutHeader = (RelativeLayout) findViewById(R.id.relativeLayoutHeader);
		headerManager = new HeaderManager(SettingsActivity.this);
		headerManager.getHeader(relativeLayoutHeader);
		tableLayout = (LinearLayout) findViewById(R.id.linear);
	
		createRadioButton1();
		spinnerMiles = (Spinner) findViewById(R.id.spinnerMiles);
		/*radioGroupLeft = (RadioGroup) findViewById(R.id.radiogroupLeft);
		radioGroupRight = (RadioGroup) findViewById(R.id.radioGroupRight);

		radioBuddhism = (RadioButton) findViewById(R.id.checkBhuddhism);
		radioChrisitian = (RadioButton) findViewById(R.id.checkChristian);
		radioJudasim = (RadioButton) findViewById(R.id.checkJudasim);
		radioHinduism = (RadioButton) findViewById(R.id.checkHinduism);
		radioSikhism = (RadioButton) findViewById(R.id.checksikhism);
		radioIslam = (RadioButton) findViewById(R.id.checkIslam);
*/
		headerManager.getImageLeft().setVisibility(View.INVISIBLE);
		headerManager.getButtonRight().setOnClickListener(this);

	/*	radioGroupLeft.setTag(1);
		radioGroupRight.setTag(2);
		radioGroupLeft.setOnCheckedChangeListener(this);
		radioGroupRight.setOnCheckedChangeListener(this);*/
		
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.miles_array));
			dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinnerMiles.setAdapter(dataAdapter);
			String item = AppPreferenceManager.getMiles(this);
			int position=0;
			if(item.equals("5"))
			{
				position =0;
			}
			else if(item.equals("10"))
			{
				position =1;
			}
			else if(item.equals("20"))
			{
				position =2;
			}
			else if(item.equals("50"))
			{
				position =3;
			}
			else if(item.equals("100"))
			{
				position =4;
			}
			spinnerMiles.setSelection(position);
	}
	private void createRadioButton1() {
		int size =AppPreferenceManager.getFaithList(this).size();
		int column = 2;
		int count = size/column;
	    final RadioButton[] rb = new RadioButton[size];
	    rg1 = new RadioGroup(this); 
	    rg2 = new RadioGroup(this);//create the RadioGroup
	    rg1.setOrientation(RadioGroup.VERTICAL);//or RadioGroup.VERTICAL
	    rg2.setOrientation(RadioGroup.VERTICAL);//or RadioGroup.VERTICAL
	    rg2.setPadding(0, 0, 30, 0);
	    rg1.setPadding(30, 0, 0, 0);
	    rg1.setLayoutParams(new RadioGroup.LayoutParams(
	    		0,
                LinearLayout.LayoutParams.WRAP_CONTENT,1f));
	    rg2.setLayoutParams(new RadioGroup.LayoutParams(
	    		0,
                LinearLayout.LayoutParams.WRAP_CONTENT,1f));
	    rg1.clearCheck(); // this is so we can start fresh, with no selection on both RadioGroups
	    rg2.clearCheck();
	    for(int i=0; i<count; i++){
	       rb[i]  = new RadioButton(this);
	       rg1.addView(rb[i]);           
	       rb[i].setText(AppPreferenceManager.getFaithList(this).get(i).getFaith().toString());
	       rb[i].setId(Integer.parseInt(AppPreferenceManager.getFaithList(this).get(i).getId()));
	       rb[i].setTextColor(Color.BLACK);
	       
	       if(AppPreferenceManager.getFaith(this).equals(AppPreferenceManager.getFaithList(this).get(i).getFaith().toString()))
	       {
	    	   rb[i].setChecked(true);
	       }
	    }
	    //you add the whole RadioGroup to the layout
	    for(int i=count; i<size; i++){
		       rb[i]  = new RadioButton(this);
		       rg2.addView(rb[i]);           
		       rb[i].setText(AppPreferenceManager.getFaithList(this).get(i).getFaith().toString());
		       rb[i].setTextColor(Color.BLACK);
		       rb[i].setId(Integer.parseInt(AppPreferenceManager.getFaithList(this).get(i).getId()));
		       
		       if(AppPreferenceManager.getFaith(this).equals(AppPreferenceManager.getFaithList(this).get(i).getFaith().toString()))
		       {
		    	   rb[i].setChecked(true);
		       }
		    }
	    tableLayout.addView(rg2);
	    tableLayout.addView(rg1);
		    
		    rg1.setOnCheckedChangeListener(this);
		    rg2.setOnCheckedChangeListener(this);  
	}
	@Override
	public void onClick(View v) {
		if (v == headerManager.getButtonRight()) {
			
			AppPreferenceManager.saveMiles(spinnerMiles.getSelectedItem().toString().replace("Miles", "").trim(), this);
			finish();
		}
	}

	
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId)
	{
		int radioButtonChecked=-1;
		if (group==rg1 && checkedId != -1) {
            rg2.setOnCheckedChangeListener(null); // remove the listener before clearing so we don't throw that stackoverflow exception(like Vladimir Volodin pointed out)
            rg2.clearCheck(); // clear the second RadioGroup!
            rg2.setOnCheckedChangeListener(this); //reset the listener
            
            radioButtonChecked = rg1.getCheckedRadioButtonId();
        }
		else if(group==rg2 && checkedId != -1)
		{
			rg1.setOnCheckedChangeListener(null);
            rg1.clearCheck();
            rg1.setOnCheckedChangeListener(this);
            radioButtonChecked = rg2.getCheckedRadioButtonId();
		}
		
		Log.e("", "Checked radio button id"+radioButtonChecked);
		ArrayList<FaithModel> faithModelArray = AppPreferenceManager.getFaithList(this);
		for (int i = 0; i < faithModelArray.size(); i++)
		{
					if(faithModelArray.get(i).getId().equals(String.valueOf(radioButtonChecked)))
					{
						AppPreferenceManager.saveFaith(faithModelArray.get(i).getFaith(),this);
						break;
					}
		}
		
		/*switch (radioButtonChecked) {
		case R.id.checkBhuddhism:
			AppPreferenceManager.saveFaith(getString(R.string.bhuddhism),
					this);
			break;
		case R.id.checkChristian:
			AppPreferenceManager.saveFaith(getString(R.string.christanity),
					this);
			break;
		case R.id.checkHinduism:
			AppPreferenceManager.saveFaith(getString(R.string.hinduism),
					this);
			break;
		default:
			break;
		}
		*/
	}

	/*@Override
	public void onCheckedChanged(RadioGroup rg, int id) {

		
		 * if(rg.getTag().equals(1)) { radioGroupRight.clearCheck(); } else {
		 * radioGroupLeft.clearCheck(); }
		 

		if (rg == radioGroupLeft) {
			if(radioGroupRight.getCheckedRadioButtonId()!=-1)
				radioGroupRight.clearCheck();
			switch (id) {
			case R.id.checkBhuddhism:
				AppPreferenceManager.saveFaith(getString(R.string.bhuddhism),
						this);
				break;
			case R.id.checkChristian:
				AppPreferenceManager.saveFaith(getString(R.string.christanity),
						this);
				break;
			case R.id.checkHinduism:
				AppPreferenceManager.saveFaith(getString(R.string.hinduism),
						this);
				break;
			default:
				break;
			}
		} else {
			if(radioGroupLeft.getCheckedRadioButtonId()!=-1)
				radioGroupLeft.clearCheck();
			switch (id) {
			case R.id.checkIslam:
				AppPreferenceManager.saveFaith(getString(R.string.islam), this);
				break;

			case R.id.checkJudasim:
				AppPreferenceManager.saveFaith(getString(R.string.judasim),
						this);
				break;
			case R.id.checksikhism:
				AppPreferenceManager.saveFaith(getString(R.string.sikhism),
						this);
				break;
			default:
				break;
			}
		}
	}*/
}